import React, { useState, useEffect } from 'react';
import {
    Button,
    Form,
    Input,
    InputNumber,
    Radio,
} from 'antd';
import { action_addlist, action_update } from '../../stores/climary'
import { useAppDispatch } from '../../stores/getStateTs';

const Myform: React.FC = (props: any) => {
    let dispatch = useAppDispatch()

    const [form] = Form.useForm();
    useEffect(() => {
        form.setFieldsValue(props.itemObj)
    })

    const onFinish = (values: any) => {
        if (props.title == '添加') {
            let obj = {
                ...values,
                "id": new Date().getTime()
            }
            dispatch(action_addlist(obj))
        } else {
            let obj = {
                ...values,
                "id": props.itemObj.id
            }
            dispatch(action_update(obj))
        }
        props.showModal()
    };

    return (
        <Form onFinish={onFinish} form={form}>
            <Form.Item label="优惠券名称" name='title'
                rules={[{ required: true, message: '优惠券名称不能为空!' }]}
            >
                <Input />
            </Form.Item>

            <Form.Item label="优惠券面值" name='mianzhi'
                rules={[{ required: true, message: '优惠券面值不能为空!' }]}
            >
                <Input />
            </Form.Item>

            <Form.Item label="优惠券最低消费" name='zuidi'
                rules={[{ required: true, message: '优惠券最低消费不能为空!' }]}
            >
                <Input />
            </Form.Item>

            <Form.Item label="优惠券有效期限（天）" name='qixian'
                rules={[{ required: true, message: '优惠券有效期限不能为空!' }]}
            >
                <Input />
            </Form.Item>

            <Form.Item label="排序" name='paixu'
                rules={[{ required: true, message: '排序不能为空!' }]}
            >
                <InputNumber />
            </Form.Item>

            <Form.Item label="状态" name='collect'
                rules={[{ required: true, message: '状态不能为空!' }]}
            >
                <Radio.Group
                    name="radiogroup"
                    options={[
                        { value: true, label: '开启' },
                        { value: false, label: '关闭' },
                    ]}
                />
            </Form.Item>

            <Form.Item>
                <Button type="primary" htmlType="submit">完成</Button>
            </Form.Item>
        </Form>
    );
};

export default Myform;